package com.example.app_test;

import java.security.PublicKey;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.media.MediaBrowserCompat.MediaItem.Flags;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import ServerConnect.GetInfo;
public class RegisterNow extends Activity{
	private EditText user = null;
	private EditText pass = null;
	private EditText pass_again = null;
	private Button button_register = null;
	//private String username,password,passwordagain;
	private String username;
	private String user_city = "shanghai";
	private String user_get,pass_get,pass_again_get,result;
	private GetInfo getinfo = new GetInfo();
	private int flag = 0;
	protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
		setContentView(R.layout.register);
		user = (EditText )findViewById (R.id.user_new);
		pass = (EditText)findViewById(R.id.pass_new);
		pass_again = (EditText)findViewById(R.id.pass_again);
		button_register = (Button)findViewById(R.id.button_register);
		
	}
	public void RegisterNow(View view){
		user_get = user.getText().toString();
		pass_get = pass.getText().toString();
		pass_again_get = pass_again.getText().toString();
		
		System.err.println("debug:"+user_get+pass_get+pass_again_get);
		if(! getinfo.get_username(user_get)) 
		{
				if( !(user_get.equals("") && pass_get.equals("")))
				{
					if(pass_get.equals(pass_again_get))
					{
						Thread thread = new Thread(){
							public void run(){
								flag=0;
								result = getinfo.registernow(user_get, pass_get,user_city);
							    flag = 1;
							}
						};
						thread.start();
						while(flag == 0){
							//System.out.println(flag);
						}
						if(result.equals("true")){
							System.err.println("debug this is result :"+result +user_get+pass_get);
							Intent toregistersuccess = new Intent(RegisterNow.this,RegisterSuccess.class);
							startActivity(toregistersuccess);
							RegisterNow.this.finish();
						}
						else{
							new AlertDialog.Builder(RegisterNow.this).setTitle("Regist Failed")
							.setMessage("Server error , check your network and retry it !")
							.setPositiveButton("YES", new DialogInterface.OnClickListener() {
								
								@Override
								public void onClick(DialogInterface arg0, int arg1) {
									// TODO Auto-generated method stub
									System.out.print("debug 7:rewrite password");
								}
							})
							.show();
						}
						
					}
					else
					{
						new AlertDialog.Builder(RegisterNow.this).setTitle("Regist Failed")
						.setMessage("Defrient password ! , rewrite it")
						.setPositiveButton("YES", new DialogInterface.OnClickListener() {
							
							@Override
							public void onClick(DialogInterface arg0, int arg1) {
								// TODO Auto-generated method stub
								System.out.print("debug 7:rewrite password");
							}
						})
						.show();
					}
					
				}
				else{
						new AlertDialog.Builder(RegisterNow.this).setTitle("Regist Failed")
						.setMessage("Null user and password ! , rewrite it")
						.setPositiveButton("YES", new DialogInterface.OnClickListener() {
							
							@Override
							public void onClick(DialogInterface arg0, int arg1) {
								// TODO Auto-generated method stub
								System.out.print("debug 7:rewrite password");
							}
						})
						.show();
					
				}
				
		}
		else{
			new AlertDialog.Builder(RegisterNow.this).setTitle("Regist Failed")
			.setMessage("username has already have! , rewrite it")
			.setPositiveButton("YES", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface arg0, int arg1) {
					// TODO Auto-generated method stub
					System.out.print("debug 7:rewrite password");
				}
			})
			.show();
		
		}
	
	}
	public void Back(View view){
		super.onBackPressed();
		this.finish();
	}
	
	
}
